<?php

//ファイル読込み
function openfile($filename){

	$content = file($filename);
	$txt = join("",$content);

	return $txt;
}


//CSV出力
function csv_write( $filename, $data ){
	
	$fp = @fopen( $filename, 'a');
	if( flock( $fp, LOCK_EX) ){ 
		
		$txt = '"' . implode('","', $data ) . '"';
		$txt .= "\n";
		
		fwrite( $fp, $txt );
		flock( $fp, LOCK_UN);
	}
	fclose( $fp );
	chmod( $filename, 0644);
	
	return;
}


//メール送信
function mail_sending($mailto, $mailfrom, $subject, $mailbody, $bcc = ''){
	
	$addheader = "From: $mailfrom";
	if($bcc != ''){
		$addheader .= "\nBcc: $bcc";
	}

	//半角ｶﾅ → 全角カナ 変換
	$mailbody = mb_convert_kana($mailbody,"KV","UTF-8");

	$result = mb_language("ja");
	mb_internal_encoding("UTF-8");
	//ISO-2022-JPに変換
	//$subject = mb_encode_mimeheader($subject, "ISO-2022-JP", "UTF-8");
	$mailbody = mb_convert_encoding( $mailbody, "ISO-2022-JP", "UTF-8" );

	$result = mb_send_mail( $mailto, $subject, $mailbody, $addheader );

	return;
}


// 拡張子取得
function getext( $filename ){
	
	if( strpos( $filename, "." ) ){
		$tmp = explode('.', $filename );
		return '.' . end( $tmp );
	}else{
		return '';
	}
	
}


/*
// 画像リサイズ・保存
function resize( $src, $dest, $width ){
	
	if( !file_exists($src) ) return false;
	
	if( eregi( ".jpg$", $dest ) ){
		$type = "jpeg";
	}elseif( eregi( ".jpeg", $dest ) ){
		$type = "jpeg";
	}elseif( eregi( ".png", $dest ) ){
		$type = "png";
	}else{
		$type = "";
	}

	if( $srcSize = getImageSize( $src ) ){

		$srcWidth  = $srcSize[0];
		$srcHeight = $srcSize[1];

		if( $type == "jpeg" ){
			$srcImage = imageCreateFromJpeg( $src );
		}elseif( $type == "png" ){
			$srcImage = imageCreateFromPng( $src );
		}

	}

	if( $width < $srcWidth ){
		
		$height = $srcHeight * $width / $srcWidth;
		$newImage = imageCreateTrueColor( $width, $height );
		imageCopyResampled( $newImage, $srcImage, 0, 0, 0, 0, $width, $height, $srcWidth, $srcHeight );
		ImageJPEG($newImage, $dest, 90);
		imageDestroy( $srcImage );
		
	}else{
		// 小さい画像だったらそのままコピー
		copy( $src, $dest );
	}
	
	return;
}
*/

// 画像リサイズ・保存
function resize( $src, $dest, $new_width, $new_height ){
	
	if( !file_exists($src) ) return false;
	
	if( eregi( ".jpg$", $dest ) ){
		$type = "jpeg";
	}elseif( eregi( ".jpeg", $dest ) ){
		$type = "jpeg";
	}elseif( eregi( ".png", $dest ) ){
		$type = "png";
	}elseif( eregi( ".gif", $dest ) ){
		$type = "gif";
	}else{
		$type = "";
	}

	if( $srcSize = getImageSize( $src ) ){

		$srcWidth  = $srcSize[0];
		$srcHeight = $srcSize[1];

		if( $type == "jpeg" ){
			$srcImage = imageCreateFromJpeg( $src );
		}elseif( $type == "png" ){
			$srcImage = imageCreateFromPng( $src );
		}elseif( $type == "gif" ){
			$srcImage = imageCreateFromGif( $src );
		}

	}
	
	if( $srcWidth > $srcHeight ){
	
		// 横長画像
		if( $new_width < $srcWidth ){
			
			$height = $srcHeight * $new_width / $srcWidth;
			$newImage = imageCreateTrueColor( $new_width, $height );
			imageCopyResampled( $newImage, $srcImage, 0, 0, 0, 0, $new_width, $height, $srcWidth, $srcHeight );
			ImageJPEG($newImage, $dest, 90);
			imageDestroy( $srcImage );
			
		}else{
			// 小さい画像だったらそのままコピー
			copy( $src, $dest );
		}

	}else{

		// 縦長画像
		if( $new_height < $srcHeight ){
			
			$width = $srcWidth * $new_height / $srcHeight;
			$newImage = imageCreateTrueColor( $width, $new_height );
			imageCopyResampled( $newImage, $srcImage, 0, 0, 0, 0, $width, $new_height, $srcWidth, $srcHeight );
			ImageJPEG($newImage, $dest, 90);
			imageDestroy( $srcImage );
			
		}else{
			// 小さい画像だったらそのままコピー
			copy( $src, $dest );
		}
		
	}

	return;
}



// パスワード生成
function create_password( $len = 10 ){
	$chars = array(
		'0', '1', '2', '3', '4', '5', '6', '7', '8', '9',
		'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j',
		'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', 's', 't',
		'u', 'v', 'w', 'x', 'y', 'z',
		'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J',
		'K', 'L', 'M', 'N', 'O', 'P', 'Q', 'R', 'S', 'T',
		'U', 'V', 'W', 'X', 'Y', 'Z',
	);

	$pw = array();

	for( $i = 0; $i < $len; $i++ ){
		shuffle($chars);
		$pw[] = $chars[rand(0, 35)];
	}
	shuffle($pw);
	$pw = implode('', $pw);

	return $pw;
}
?>
